package com.zh.freechat.assemble;

import com.zh.freechat.common.resp.ApplyRecordVO;
import com.zh.freechat.common.resp.UserContactsVO;
import com.zh.freechat.domain.user.entity.ApplyFriend;
import com.zh.freechat.domain.user.entity.ChatUser;
import com.zh.freechat.domain.user.entity.UserRelationBlack;
import com.zh.freechat.domain.user.entity.UserRelationFriend;
import com.zh.webcommon.common.support.CopyFieldUtil;
import org.springframework.util.StringUtils;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ZH
 * @date 13:50 2021/8/18
 */
public final class ApplyRecordWrapper {

    public static ApplyRecordVO convertVO(ApplyFriend entity) {
        ApplyRecordVO vo = new ApplyRecordVO();

        vo.setId(entity.getId());
        vo.setStatus(entity.getStatus());
        vo.setRemark(entity.getRemark());
        vo.setCreateTime(entity.getCreateTime());

        vo.setUserId(entity.getApplyUserId());
        vo.setUsername(entity.getApplyUser().getUsername());
        vo.setNickname(entity.getApplyUser().getNickname());
        vo.setAvatarUrl(entity.getApplyUser().getAvatarUrl());
        vo.setGender(entity.getApplyUser().getGender());

        return vo;
    }

    private static List<UserContactsVO.ContactsItem> convertItemWithFriend(List<UserRelationFriend> friends) {
        return friends.stream()
                .map(e -> {
                    ChatUser chatUser = e.getChatUser();
                    UserContactsVO.ContactsItem item = new UserContactsVO.ContactsItem(e.getId().toString(),chatUser.getUid(), chatUser.getUsername());
                    item.setAliasName(StringUtils.hasText(e.getAliasName()) ? e.getAliasName() : chatUser.getNickname());
                    item.writeFirstLetter();
                    return CopyFieldUtil.copyProperties(chatUser, item);
                })
                .sorted(Comparator.comparing(UserContactsVO.ContactsItem::getAliasName))
                .collect(Collectors.toList());
    }

    private static List<UserContactsVO.ContactsItem> convertItemWithBlack(List<UserRelationBlack> blacks) {
        return blacks.stream()
                .map(e -> {
                    UserContactsVO.ContactsItem item = new UserContactsVO.ContactsItem(e.getId().toString(),e.getBlackId(), e.getBlackId());
                    item.writeFirstLetter();
                    return item;
                })
                .collect(Collectors.toList());
    }
}
